package com.example.springboot.service;

import cn.hutool.core.util.StrUtil;
import com.example.springboot.entity.*;
import com.example.springboot.mapper.SysMenuMapper;
import com.example.springboot.mapper.SysRoleMapper;
import com.example.springboot.mapper.SysRoleMenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: jyf2000jyf
 * @Date: 2022/5/3
 * @Time: 14:50
 * @Email: 904593011@qq.com
 * @Description:
 **/
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    SysRoleMapper roleMapper;

    @Autowired
    SysRoleMenuMapper roleMenuMapper;

    @Autowired
    SysMenuMapper menuMapper;

    @Override
    public Integer save(SysRole role) {
        if (role.getId() == null) {  // role没有id，则表示是新增
            return roleMapper.insertSelective(role);
        } else { // 否则为更新
            return roleMapper.updateByPrimaryKeySelective(role);
        }
    }

    @Override
    public List<SysRole> findAll() {
        return roleMapper.selectByExample(null);
    }

    @Override
    public Integer delete(Integer id) {
        return roleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<SysRole> findAll(Integer pageNum, Integer pageSize) {
        SysRoleExample example = new SysRoleExample();
        SysRoleExample.Criteria criteria = example.createCriteria();
        example.setOrderByClause("id desc limit " + pageNum + "," + pageSize);
        return roleMapper.selectByExample(example);
    }

    @Override
    public Integer getAmount() {
        return (Integer) (int) roleMapper.countByExample(null);
    }

    @Override
    public void saveBatch(List<SysRole> roles) {
        roleMapper.deleteByExample(null);
        for (SysRole role : roles) {
            roleMapper.insertSelective(role);
        }
    }

    @Override
    public void setRoleMenu(Integer roleId, List<Integer> menuIds) {
        SysRoleMenuExample example = new SysRoleMenuExample();
        example.createCriteria().andRoleIdEqualTo(roleId);
        roleMenuMapper.deleteByExample(example);
        SysRoleMenuKey roleMenu = new SysRoleMenuKey();
        roleMenu.setRoleId(roleId);
        for (Integer menuId : menuIds) {
            roleMenu.setMenuId(menuId);
            roleMenuMapper.insert(roleMenu);
        }
    }

    @Override
    public List<Integer> getRoleMenu(Integer roleId) {
        SysRoleMenuExample example = new SysRoleMenuExample();
        example.createCriteria().andRoleIdEqualTo(roleId);
        List<SysRoleMenuKey> sysRoleMenuKeys = roleMenuMapper.selectByExample(example);
        List<Integer> menuIds = new ArrayList<>();
        for (SysRoleMenuKey sysRoleMenuKey : sysRoleMenuKeys) {
            if (StrUtil.isBlank(menuMapper.selectByPrimaryKey(sysRoleMenuKey.getMenuId()).getPath())) {
                continue;
            }
            menuIds.add(sysRoleMenuKey.getMenuId());
        }
        return menuIds;
    }
}
